<?php
/**
 * 创蓝网短信发送接口
 * User: yu
 * Date: 16-11-14
 * Time: 下午3:40
 */
namespace utils\sms;

class ChuangLanProvider
{
    private $user = null;
    private $pass = null;
    private $key = null;
    private $sign = null;
    private $authed = false;

    public function auth($user, $pass, $key, $sign)
    {
        $this->user = $user;
        $this->pass = $pass;
        $this->key = $key;
        $this->sign = $sign;
        $this->authed=true;
    }


    /**
     * 发送短信
     *
     * @param string $mobile        手机号码
     * @param string $msg           短信内容
     */
    public function send( $mobile, $content) {

        //创蓝接口参数
        $postArr = array (
            'account' => $this->user,
            'pswd' => $this->pass,
            'msg' => $content,
            'mobile' => $mobile,
            'needstatus' => true
        );

        $result = $this->curlPost( 'http://222.73.117.156/msg/HttpBatchSendSM' , $postArr);
        //http://222.73.117.156/msg/HttpBatchSendSM
        //状态码：　117	IP地址认证错,请求调用的IP地址不是系统登记的IP地址
        return $result;
    }


    /**
     * 处理返回值
     *
     */
    public function execResult($result){
        $result=preg_split("/[,\r\n]/",$result);
        return $result;
    }

    /**
     * 通过CURL发送HTTP请求
     * @param string $url  //请求URL
     * @param array $postFields //请求参数
     * @return mixed
     */
    private function curlPost($url,$postFields){
        $postFields = http_build_query($postFields);
        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_POST, 1 );
        curl_setopt ( $ch, CURLOPT_HEADER, 0 );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $postFields );
        $result = curl_exec ( $ch );
        curl_close ( $ch );
        return $result;
    }

    //魔术获取
    public function __get($name){
        return $this->$name;
    }

    //魔术设置
    public function __set($name,$value){
        $this->$name=$value;
    }
}

?>